package com.example.manager.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manager.service.SysRoleService;
import com.example.model.dto.system.AssginMenuDto;
import com.example.model.dto.system.AssginRoleDto;
import com.example.model.dto.system.SysRoleDto;
import com.example.model.entity.system.SysRole;
import com.example.model.vo.common.Result;
import com.example.model.vo.common.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation(value = "分页查询")
    @PostMapping("/findByPage")
    //swagger头部携带token，swagger测试接口时候使用
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result findByPage(@RequestBody SysRoleDto sysRoleDto) {
        Page<SysRole> data = sysRoleService.findByPage(sysRoleDto);
        return Result.build(data , ResultCodeEnum.SUCCESS);
    }


    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result saveOrUpdate(@RequestBody SysRole sysRole) {
        sysRoleService.saveOrUpdate(sysRole);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result batchDelete(@RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/findById/{id}")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result findById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.build(sysRole , ResultCodeEnum.SUCCESS);
    }


    @ApiOperation(value = "查询所有角色信息")
    @GetMapping("/allRoles/{userId}")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result allRoles(@PathVariable Long userId) {

        Map<String, Object> map = sysRoleService.allRoles(userId);
        return Result.build(map , ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "分配角色")
    @PostMapping("/assigningRoles")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result assigningRoles(@RequestBody AssginRoleDto assginRoleDto) {

        sysRoleService.assigningRoles(assginRoleDto);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "根据角色id查询出所有菜单id")
    @GetMapping("/findMenuIdsByRoleId/{roleId}")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result findMenuIdsByRoleId(@PathVariable Long roleId) {
        List<Long> menuIds = sysRoleService.findMenuIdsByRoleId(roleId);
        return Result.build(menuIds , ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "分配菜单")
    @PostMapping("/assigningMenus")
    @ApiImplicitParam(name = "token" ,value = "token值"  ,dataType = "String",paramType = "header")
    public Result assigningMenus(@RequestBody AssginMenuDto assginMenuDto) {

        sysRoleService.assigningMenus(assginMenuDto);
        return Result.build(null , ResultCodeEnum.SUCCESS);

    }


}
